package com.ff.xyh.service.complaint.controller;


import com.ff.xyh.common.entity.complaint.ComplaintHandle;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.qo.complaint.ComplaintInfoQuery;
import com.ff.xyh.common.entity.complaint.ComplaintInfo;
import com.ff.xyh.service.complaint.service.ComplaintInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 投诉信息表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2023-05-24
 */
@RestController
@RequestMapping("/complaint-service")
public class ComplaintInfoController {


    @Autowired
    private ComplaintInfoService complaintInfoService;

    /**
     * 举报信息 - 条件查询，包含分页
     * @param complaintInfoQuery
     * @return
     */
    @PostMapping("get-complaint-info-condition")
    public ResultObject getComplaintInfoCondition(
            @RequestBody ComplaintInfoQuery complaintInfoQuery) {
        return complaintInfoService
                .getComplaintInfoCondition(complaintInfoQuery);
    }
    /**
     * 举报信息 - id 查询
     * @param id
     * @return
     */
    @PostMapping("get-complaint-info-id")
    public ResultObject getComplaintInfoById(
            @RequestParam("id") String id) {
        ComplaintInfo byId = complaintInfoService.getById(id);

        return ResultObject.ok().putData(byId);
    }

    /**
     * 举报信息 - 根据 id 删除
     * @param id
     * @return
     */
    @PostMapping("delete-complaint-info-id")
    public ResultObject deleteComplaintInfoById(
            @RequestParam("id") String id) {
        if (complaintInfoService.getById(id) == null) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        } else {
            if (complaintInfoService.removeById(id)) {
                return ResultObject.ok();
            } else {
                return ResultObject.ok().message(ResultMessage.FAILED_TO_DELETE);
            }
        }
    }

    /**
     * 举报信息 - 添加
     * @param complaintInfo
     * @return
     */
    @PostMapping("add-complaint-info")
    public ResultObject addComplaintInfo(
            @RequestBody ComplaintInfo complaintInfo) {
        if (!StringUtils.isEmpty(complaintInfo.getId())) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        }
        if (complaintInfoService.save(complaintInfo)) {
            return ResultObject.ok().putData(complaintInfo);
        } else {
            return ResultObject.ok().message(ResultMessage.FAILED_TO_INSERT);
        }

    }
    /**
     * 举报信息 - 修改
     * @param complaintInfo
     * @return
     */
    @PostMapping("modify-complaint-info")
    public ResultObject modifyComplaintInfo(
            @RequestBody ComplaintInfo complaintInfo) {
        if (StringUtils.isEmpty(complaintInfo.getId())
                || complaintInfoService.getById(complaintInfo.getId()) == null) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        }
        if (complaintInfoService.updateById(complaintInfo)) {
            return ResultObject.ok().putData(complaintInfo);
        } else {
            return ResultObject.ok().message(ResultMessage.FAILED_TO_UPDATE);
        }
    }


    /**
     * 举报信息 - 处理
     * @param complaintHandle
     * @return
     */
    @PostMapping("handle-complaint-info")
    public ResultObject handleComplaintInfo(
            @RequestBody ComplaintHandle complaintHandle) {
        return complaintInfoService.handleComplaintInfo(complaintHandle);
    }


}

